﻿using Microsoft.AspNetCore.Mvc;
using Sgr.Application.Queries;
using Sgr.AspNetCore.ActionFilters.AuditLogs;
using Sgr.AspNetCore.ActionFilters.Permissions;
using Sgr.AuditLogs.Model;
using Sgr.AuditLogs.Services;
using Sgr.Workspace.Models;
using Sgr.Workspace.Services;
using Sgr.Workspace.Services.Impl;
using Sgr.Workspace.ViewModels;

namespace Sgr.Workspace.Controllers
{
    /// <summary>
    /// 工作台统计接口
    /// </summary>
    [Route("api/v1/sgr/workspace")]
    [ApiController]
    [Produces("application/json")]
    [Tags("Workspace")]
    public class WorkspaceController : ControllerBase
    {
        private readonly IAppAnalysisService _appAnalysisService;

        public WorkspaceController(IAppAnalysisService appAnalysisService)
        {
            _appAnalysisService = appAnalysisService;
        }

        /// <summary>
        /// 获得运维概况
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        [CheckPermission(Permissions.QueryOPS)]
        [HttpPost("ops-overview")]
        [ProducesResponseType(typeof(OpsOverviewViewModel), StatusCodes.Status200OK)]
        [AuditLogActionFilter("获得运维概况")]
        public async Task<ActionResult<OpsOverviewViewModel>> GetOpsOverview(CancellationToken cancellationToken)
        {
            var data = await _appAnalysisService.GetOpsOverviewAsync(cancellationToken);
            return Ok(data);
        }

        /// <summary>
        /// 获取当前在线用户数
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        [CheckPermission(Permissions.QueryOPS)]
        [HttpGet("current-online-users")]
        [ProducesResponseType(typeof(OnlineUserViewModel), StatusCodes.Status200OK)]
        [AuditLogActionFilter("获取当前在线用户数")]
        public async Task<ActionResult<OnlineUserViewModel>> GetCurrentOnlineUserCount(CancellationToken cancellationToken)
        {
            var data = await _appAnalysisService.GetCurrentOnlineUserCountAsync(cancellationToken);
            return Ok(new OnlineUserViewModel()
            {
                CurrentOnlineUserCount = data?.Current ?? 0,
                PreOnlineUserCount = data?.Previous ?? 0,
            });
        }
    }
}